package com.it9.taotao.manager.controller;

import com.it9.taotao.base.EasyUIImageUploadBean;
import com.it9.taotao.base.EasyUIPageDatasBean;
import com.it9.taotao.manager.pojo.TbItem;
import com.it9.taotao.manager.service.IGoodsService;
import com.it9.taotao.manager.utils.FastDFSClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@Controller
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private FastDFSClient fastDFSClient;

    @GetMapping("{jspentity}")
    public String toItemList(@PathVariable("jspentity") String jspentity) {
        return jspentity;
    }

    @PostMapping("/pic/upload")
    @ResponseBody
    public EasyUIImageUploadBean imageUpload(@RequestParam("uploadFile") MultipartFile uploadFile) {
        EasyUIImageUploadBean easyUIImageUploadBean = new EasyUIImageUploadBean();
        try {
            String path = fastDFSClient.uploadFile(uploadFile.getBytes());
            easyUIImageUploadBean.setError(0);
            easyUIImageUploadBean.setMessage("上传成功");
            easyUIImageUploadBean.setUrl(path);
        } catch (Exception e) {
            e.printStackTrace();
            easyUIImageUploadBean.setError(1);
            easyUIImageUploadBean.setMessage("服务器繁忙,请稍后重试");
        }

        return easyUIImageUploadBean;
    }

    @GetMapping("/item/list")
    @ResponseBody
    public EasyUIPageDatasBean<TbItem> listTbItem(@RequestParam(required = false, defaultValue = "1") String page,
                                          @RequestParam(required = false, defaultValue = "30") String rows) {

        EasyUIPageDatasBean<TbItem> goodsPagination = goodsService.getGoodsPagination(Integer.parseInt(page), Integer.parseInt(rows));
        return goodsPagination;
    }
}
